<?php

/**
 * Path class
 *
 * @author Julot <andy.julot@gmail.com>
 * @since Tuesday, June 21, 2011. 02:57 PM
 */
class Path extends CComponent {

	protected $_path;
	protected $_dirname;
	protected $_basename;
	protected $_filename;
	protected $_extension;
	protected $_isImage = null;
	protected $_imageExtensions = array('bmp', 'gif', 'jpg', 'jpeg', 'png');

	/**
	 * @param string $path to file.
	 */
	public function __construct($path) {
		$this->_path = $path;
		$info = pathinfo($path);
		$this->_dirname = $info['dirname'];
		$this->_basename = $info['basename'];
		$this->_filename = $info['filename'];
		if (isset($info['extension'])) $this->_extension = $info['extension'];
	}

	public function getPath() {
		return $this->_path;
	}

	/**
	 * @return string directory name
	 */
	public function getDirname() {
		return $this->_dirname;
	}

	/**
	 * @return string path name without extension and directory.
	 */
	public function getFilename() {
		return $this->_filename;
	}

	/**
	 * @return string string path name with extension without directory.
	 */
	public function getBasename() {
		return $this->_basename;
	}

	/**
	 * @return string extension name of path
	 */
	public function getExtension() {
		return $this->_extension;
	}

	/**
	 * Check whether path is exist
	 * @return boolean whether the path exists
	 */
	public function isExists() {
		return file_exists($this->_path);
	}

	/**
	 * Publish the path (directory or file)
	 * @return string an absolute URL to the published asset
	 * @see CAssetManager
	 */
	public function publish() {
		return Yii::app()->assetManager->publish($this->_path);
	}

	/**
	 * Create breadcrumbs links.
	 * In the future we may need to append more parameter such as target and
	 * rootLabel.
	 *
	 * @param string $path
	 * @return array links for CBreadcrumbs
	 */
	public static function createBreadcrumbsLinks($path) {
		$links = array('Root'=>array('index'));
		if ($path) {
			$parents = explode('/', $path);
			$bread = '';
			$multiplier = 0;
			foreach ($parents as $breadcrumb) {
				$bread .= ( $bread ? '/' : '') . $breadcrumb;
				if (array_key_exists($breadcrumb, $links)) {
					$multiplier++;
					$breadcrumb .= str_repeat(' ', $multiplier);
				}
				$links[$breadcrumb] = array(
					'index',
					'path'=>$bread
				);
			}
		}
		return $links;
	}

	/**
	 * Delete the file.
	 *
	 * @return boolean whether operations succeeds.
	 */
	public function delete() {
		if (unlink($this->_path)) {
			if (file_exists($this->thumbnailPath)) unlink($this->thumbnailPath);
			return true;
		}
		return false;
	}

	/**
	 * Rename path.
	 *
	 * @param string $newPath absolute path to new name
	 * @return boolean wether operations succeeds.
	 */
	public function rename($newPath) {
		if (file_exists($newPath)) return false;

		if (rename($this->_path, $newPath)) {
			if (file_exists($this->thumbnailPath)) unlink($this->thumbnailPath);
			$this->_path = $newPath;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Check whether path is an image
	 * @return boolean whether the path is image
	 */
	public function isImage() {
		if ($this->_isImage === null) {
			$this->_isImage = false;
			$ext = strtolower($this->extension);
			if (array_search($ext, $this->_imageExtensions)) {
				if (getimagesize($this->_path)) {
					if ($this->createImage()) $this->_isImage = true;
				}
			}
		}
		return $this->_isImage;
	}

	public function isThumbnail() {
		if (strpos($this->_path, '.thumb.') === false) return false;
		return true;
	}

	/**
	 * @property string path of the thumbnail. Please note that thumbnail always
	 * jpg.
	 */
	public function getThumbnailPath() {
		return $this->dirname . '/' . $this->filename . '.thumb.jpg';
	}

}
